﻿using System;
using System.Data.Linq;
using System.Linq;
using System.Web.Mvc;
using GiaoXuLanTranh_MucVu.Models;
using InfoTech.Core.Data.SqlClient;
using InfoTech.LanTranh.MucVu.Bussiness;
using InfoTech.LanTranh.MucVu.Service;
using InfoTech.Mvc.JqGrid;
using InfoTech.Mvc.JqGrid.LINQ;
using InfoTech.Mvc.LanTranh.MucVu.Models;

namespace GiaoXuLanTranh_MucVu.Areas.AdminCP.Controllers
{
    [Authorize]
    public class SystemInfoController : Controller
    {
        public ActionResult General()
        {
            Header header = new Header();
            header.PageTitle = "Cấu Hình Hệ Thống";
            header.PageHeader = "Cấu Hình Hệ Thống";
            header.Menulv1 = "05";
            header.Menulv2 = "0501";
            ViewData[Header.Key] = header;

            CONFIGURATION configuration_GIAOPHAN = new CONFIGURATION();
            configuration_GIAOPHAN.ID = CONFIGURATION.DEFAULT_GIAOPHAN;
            configuration_GIAOPHAN.Select();
            if (configuration_GIAOPHAN.IsPersistence)
            {
                ViewData["DEFAULT_GIAOPHAN"] = configuration_GIAOPHAN.VALUE;
            }

            CONFIGURATION configuration_GIAOHAT = new CONFIGURATION();
            configuration_GIAOHAT.ID = CONFIGURATION.DEFAULT_GIAOHAT;
            configuration_GIAOHAT.Select();
            if (configuration_GIAOHAT.IsPersistence)
            {
                ViewData["DEFAULT_GIAOHAT"] = configuration_GIAOHAT.VALUE;
            }

            CONFIGURATION configuration_GIAOXU = new CONFIGURATION();
            configuration_GIAOXU.ID = CONFIGURATION.DEFAULT_GIAOXU;
            configuration_GIAOXU.Select();
            if (configuration_GIAOXU.IsPersistence)
            {
                ViewData["DEFAULT_GIAOXU"] = configuration_GIAOXU.VALUE;
            }

            return View();
        }

        public ActionResult Appearance()
        {
            Header header = new Header();
            header.PageTitle = "Apprearance";
            header.PageHeader = "Choose Apprearance";
            header.Menulv1 = "05";
            header.Menulv2 = "0502";
            ViewData[Header.Key] = header;

            return View();
        }

        [HttpPost]
        public ActionResult Appearance(string theme)
        {
            Header header = new Header();
            header.PageTitle = "Apprearance";
            header.PageHeader = "Choose Apprearance";
            header.Menulv1 = "05";
            header.Menulv2 = "0502";
            ViewData[Header.Key] = header;

            Session["Theme"] = theme;
            return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult CauHinhMucVu(string MAGIAOPHAN, string MAGIAOHAT, string MAGIAOXU)
        {
            CONFIGURATION configuration_GIAOPHAN = new CONFIGURATION();
            configuration_GIAOPHAN.ID = CONFIGURATION.DEFAULT_GIAOPHAN;
            configuration_GIAOPHAN.Select();
            if (configuration_GIAOPHAN.IsPersistence)
            {
                configuration_GIAOPHAN.VALUE = MAGIAOPHAN;
                configuration_GIAOPHAN.UpdatebyId();
            }

            CONFIGURATION configuration_GIAOHAT = new CONFIGURATION();
            configuration_GIAOHAT.ID = CONFIGURATION.DEFAULT_GIAOHAT;
            configuration_GIAOHAT.Select();
            if (configuration_GIAOHAT.IsPersistence)
            {
                configuration_GIAOHAT.VALUE = MAGIAOHAT;
                configuration_GIAOHAT.UpdatebyId();
            }

            CONFIGURATION configuration_GIAOXU = new CONFIGURATION();
            configuration_GIAOXU.ID = CONFIGURATION.DEFAULT_GIAOXU;
            configuration_GIAOXU.Select();
            if (configuration_GIAOXU.IsPersistence)
            {
                configuration_GIAOXU.VALUE = MAGIAOXU;
                configuration_GIAOXU.UpdatebyId();
            }

            EXCEPTION exception = new EXCEPTION();
            exception.UPDATETYPE = UpdateType.UPDATE;
            exception.RESULTTYPE = ResultType.SUCESS;
            exception.MESSAGE = "Cập nhật thành công";
            exception.Insert();
            TempData["Result"] = exception.EXCEPTIONID;

            return RedirectToAction("General");
        }

        public ActionResult Profile()
        {
            Header header = new Header();
            header.PageTitle = "My Profile";
            header.PageHeader = "My Profile";
            header.Menulv1 = "05";
            header.Menulv2 = "0503";
            ViewData[Header.Key] = header;

            USERModel userModel = Session["User"] as USERModel;

            return View(userModel);
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Profile(USERModel userModel)
        {
            Header header = new Header();
            header.PageTitle = "My Profile";
            header.PageHeader = "My Profile";
            header.Menulv1 = "05";
            header.Menulv2 = "0503";
            ViewData[Header.Key] = header;

            if (ModelState.IsValid)
            {
                EXCEPTION ex = new EXCEPTION();
                ex.UPDATETYPE = UpdateType.INSERT;
                if (userModel.PASSWORD == userModel.RETYPEPASSWORD)
                {
                    USER user = userModel;
                    USER temp = new USER();
                    temp.USERID = user.USERID;
                    temp.SelectbyId();
                    if (temp.IsPersistence)
                    {
                        user.PASSWORD = temp.PASSWORD;
                        userModel.OLD_PASWORD = temp.PASSWORD;
                    }
                    if (!string.IsNullOrEmpty(userModel.PASSWORD))
                        user.PASSWORD = Encrypt.Encrypte(userModel.PASSWORD);

                    if (USERService.Check_Update_USER(userModel))
                    {
                        Exception exception;

                        user.UpdatebyId(out exception);
                        if (exception == null)
                        {
                            //ModelState["NAME"].Value = new ValueProviderResult(null, string.Empty, CultureInfo.InvariantCulture);			
                            //userModel.NAME = "";
                            ex.RESULTTYPE = ResultType.SUCESS;
                            ex.MESSAGE = "Cập nhật thành công";
                        }
                        else
                        {
                            ex.MESSAGE = exception.Message;
                            ex.RESULTTYPE = ResultType.ERROR;
                        }
                    }
                    else
                    {
                        ex.RESULTTYPE = ResultType.ATTENTION;
                        ex.MESSAGE = "Thông tin này đã tồn tại, hoặc mật khẩu không giống mật khẩu cũ. Vui lòng thử lại sau.";
                    }
                }
                else
                {
                    ex.MESSAGE = "Password's not match.";
                    ex.RESULTTYPE = ResultType.ERROR;
                }
                ex.Insert();
                ViewData["Result"] = ex.EXCEPTIONID;
            }
            return View(userModel);
        }

    }
}
